package com.ww.scm.service;



import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.ww.core.test.spring.SpringContextTestCase;
import com.ww.core.test.utils.DataUtils;
import com.ww.core.utils.DateUtils;
import com.ww.scm.constants.Constants;
import com.ww.scm.entity.mat.Bill;
import com.ww.scm.entity.mat.Storage;
import com.ww.scm.valueObject.croe.ResponseInfo;
import com.ww.scm.valueObject.croe.ResponseJson;
import com.ww.scm.valueObject.mat.BillDetailVo;
import com.ww.scm.valueObject.mat.BillVo;

@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class StorageManagerTestCase extends SpringContextTestCase{

	private static final String TEST_BILL_TYPE_ID_1="C7403048BF6C430D8DE9F737E9690DDB";

	private static final String TEST_MAT_ID_1="C67ECD810D914231A531ACBB1E9C2F68";
	
	//private static final String TEST_MAT_STATE_ID_1="29D1B4E71CDC461DAA81182CC40FAAF5";
	
	private static final String TEST_OPERATOR1="1001";
	
	@Autowired
	private StorageManager storageManager;

	public void setStorageManager(StorageManager storageManager) {
		this.storageManager = storageManager;
	}
	

	
	@BeforeClass
	//方法在整个类初始化之后调用，一般用来做测试准备工作。
	public static void setUpBeforeClass() throws Exception {

	}

	@AfterClass
	//方法在整个类结束之前调用，一般作测试的清理工作。
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	//方法在测试方法前调用，一般用来做测试准备工作。
	public void setUp() throws Exception {
		//运行测试方法前删除所有单据
		List<Bill> billList=storageManager.findBill();
		for (Bill bill : billList) {
			//storageManager.deleteBill(bill, TEST_OPERATOR1);
		}
	}

	@After
	//方法在测试方法后调用，一般作测试的清理工作。
	public void tearDown() throws Exception {
		
	}
	
	private static String billId;
	
	@Test
    public void save() {
		String dataset="{\"dataset\":{\"billVo\":{\"id\":\"\",\"billDate\":\"20110601\",\"operator\":\"\",\"transactionId\":\"\",\"transactionTypeId\":\"1\",\"billTypeId\":\"C7403048BF6C430D8DE9F737E9690DDB\",\"billCode\":\"由系统自动生成\",\"fillTime\":\"\",\"billTypeName\":\"\",\"originEntityName\":\"\",\"targetEntityName\":\"\",\"originEntityId\":\"\",\"targetEntityId\":\"\",\"otherDic1Id\":\"24427F07B81E46C5A41AF34B62309255\",\"otherDic2Id\":\"\",\"otherDic3Id\":\"\",\"otherDic4Id\":\"\",\"otherDic5Id\":\"\",\"otherDic6Id\":\"\",\"otherDic7Id\":\"\",\"otherDic8Id\":\"\",\"otherDic9Id\":\"\",\"otherDic10Id\":\"\",\"otherDic1Name\":\"工程机\",\"otherDic2Name\":\"未选择...\",\"otherDic3Name\":\"\",\"otherDic4Name\":\"\",\"otherDic5Name\":\"\",\"otherDic6Name\":\"\",\"otherDic7Name\":\"\",\"otherDic8Name\":\"\",\"otherDic9Name\":\"\",\"otherDic10Name\":\"\",\"remark\":\"\",\"sysFlag\":\"1\",\"creator\":\"\",\"createTime\":\"\",\"lastModifier\":\"\",\"lastModifiedTime\":\"\",\"billDetailVo\":[{\"transactionDetailTypeId\":\"1\",\"materielId\":\"C67ECD810D914231A531ACBB1E9C2F68\",\"unitId\":\"0b36322c9b4911e0b2ac00ea010d55c4\",\"sysFlag\":\"1\",\"materielName\":\"KFR-23GW\",\"otherDic1Id\":\"7BE4DB1DA2E04ED5B67139AB54592096\",\"otherDic1Name\":\"活动机\",\"unitName\":\"台\",\"amount\":1,\"money\":2000,\"price\":2000,\"id\":\"\",\"creator\":\"\"}]}}}";
    	ResponseInfo responseInfo=new ResponseInfo();
    	ResponseJson<BillVo> responseJson=new ResponseJson<BillVo>(dataset);
    	BillVo billVo=responseJson.toEntity(BillVo.class, "billVo");
    	//todo:做保存,要考虑下saveBill中逻辑需要更改不,大家要调试下???
    	storageManager.saveBill(billVo);

    }
	
	
	@Test
	public void saveBillAdd2(){
		
		Date now=new Date();
		
		//单据1
		for (int i = 10; i > 1; i--) {
			
			//单据1
			BillVo bill1=new BillVo();
			bill1.setBillCode("testCode"+DataUtils.randomId());
			bill1.setBillDate(DateUtils.formatDate(now));
			bill1.setFillTime(DateUtils.formatDate(now));
			bill1.setBillTypeId(TEST_BILL_TYPE_ID_1);
			bill1.setTransactionTypeId(Constants.TRANSACTION_TYPE_ADD_ID);
			bill1.setOperator("1001");
			bill1.setOtherDic1Id("3EE6EFAD7F0C496AA8963DDE7DFBB6C1");
			bill1.setOtherDic2Id("FF85E8FB01704DA0AFC9825D9BA6A832");
			for (int j = 1; j <= 10-i; j++) {
			//单据明细1
			BillDetailVo billDetail1=new BillDetailVo();
			billDetail1.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_NONE_ID);
			billDetail1.setMaterielId(TEST_MAT_ID_1);
			billDetail1.setOtherDic1Id("8560AEB22E5D40C89BD0FE3138CD1224");
			billDetail1.setMoney(56000.0);
			billDetail1.setPrice(1250.0);
			billDetail1.setAmount(200.0);
			//billDetail1.setStateId(TEST_MAT_STATE_ID_1);
			bill1.addBillDetail(billDetail1);
			
			//单据明细2
			BillDetailVo billDetail2=new BillDetailVo();
			billDetail2.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_NONE_ID);
			billDetail2.setMaterielId(TEST_MAT_ID_1);
			billDetail2.setOtherDic1Id("8560AEB22E5D40C89BD0FE3138CD1224");
			billDetail2.setMoney(156000.0);
			billDetail2.setPrice(1250.0);
			billDetail2.setAmount(300.0);
			//billDetail2.setStateId(TEST_MAT_STATE_ID_1);
			bill1.addBillDetail(billDetail2);
			}

			
			//保存单据
			Bill billPo1=storageManager.saveBill(bill1);
		}

		
	}
	
	@Test
	public void saveBillAdd1(){
		
		Date now=new Date();
		
		//单据1
		BillVo bill1=new BillVo();
		bill1.setBillCode("testCode"+DataUtils.randomId());
		bill1.setBillDate(DateUtils.formatDate(now));
		bill1.setFillTime(DateUtils.formatDate(now));
		bill1.setBillTypeId(TEST_BILL_TYPE_ID_1);
		bill1.setTransactionTypeId(Constants.TRANSACTION_TYPE_ADD_ID);
		bill1.setOperator("1001");

		

		//单据明细1
		BillDetailVo billDetail1=new BillDetailVo();
		billDetail1.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_NONE_ID);
		billDetail1.setMaterielId(TEST_MAT_ID_1);
		billDetail1.setAmount(200.0);
		//billDetail1.setStateId(TEST_MAT_STATE_ID_1);
		bill1.addBillDetail(billDetail1);
		
		//单据明细2
		BillDetailVo billDetail2=new BillDetailVo();
		billDetail2.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_NONE_ID);
		billDetail2.setMaterielId(TEST_MAT_ID_1);
		billDetail2.setAmount(300.0);
		//billDetail2.setStateId(TEST_MAT_STATE_ID_1);
		bill1.addBillDetail(billDetail2);

		
		//保存单据
		Bill billPo1=storageManager.saveBill(bill1);
	
		 billId=billPo1.getId();
		
			Storage storage=storageManager.findBillByMaterielId(TEST_MAT_ID_1);
			assertNull(storage);
	}
	
	@Test
	public void saveBillSubmit1(){
		
		Date now=new Date();
		
		//单据2
		BillVo bill2=new BillVo();
		bill2.setId(billId);
		bill2.setBillCode("testCode"+DataUtils.randomId());
		bill2.setBillDate(DateUtils.formatDate(now));
		bill2.setFillTime(DateUtils.formatDate(now));
		bill2.setBillTypeId(TEST_BILL_TYPE_ID_1);
		bill2.setTransactionTypeId(Constants.TRANSACTION_TYPE_SUBMIT_ID);
		bill2.setOperator("1001");
		
		//单据明细3
		BillDetailVo billDetail3=new BillDetailVo();
		billDetail3.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_OUT_ID);
		billDetail3.setMaterielId(TEST_MAT_ID_1);
		billDetail3.setAmount(1200.0);
		//billDetail3.setStateId(TEST_MAT_STATE_ID_1);
		bill2.addBillDetail(billDetail3);
		
		//单据明细4
		BillDetailVo billDetail4=new BillDetailVo();
		billDetail4.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_OUT_ID);
		billDetail4.setMaterielId(TEST_MAT_ID_1);
		billDetail4.setAmount(1300.0);
		//billDetail4.setStateId(TEST_MAT_STATE_ID_1);
		bill2.addBillDetail(billDetail4);
		
	

		Bill billPo2=storageManager.saveBill(bill2);

		assertEquals(1,storageManager.findStorageBy("materiel.id", TEST_MAT_ID_1).size());
		Storage storage=storageManager.findBillByMaterielId(TEST_MAT_ID_1);
		assertNotNull(storage);
		assertEquals(Double.valueOf("2500.0"),storage.getAmount());
	}

	@Test
	public void saveBillAudit1(){
		Date now=new Date();

		//单据2
		BillVo bill3=new BillVo();
		bill3.setId(billId);
		bill3.setBillCode("testCode"+DataUtils.randomId());
		bill3.setBillDate(DateUtils.formatDate(now));
		bill3.setFillTime(DateUtils.formatDate(now));
		bill3.setBillTypeId(TEST_BILL_TYPE_ID_1);
		bill3.setTransactionTypeId(Constants.TRANSACTION_TYPE_AUDIT_ID);
		bill3.setOperator("1001");
		
		//单据明细5
		BillDetailVo billDetail5=new BillDetailVo();
		billDetail5.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_OUT_ID);
		billDetail5.setMaterielId(TEST_MAT_ID_1);
		billDetail5.setAmount(11200.0);
		//billDetail5.setStateId(TEST_MAT_STATE_ID_1);
		bill3.addBillDetail(billDetail5);
		
		//单据明细6
		BillDetailVo billDetail6=new BillDetailVo();
		billDetail6.setTransactionDetailTypeId(Constants.TRANSACTION_DETAIL_TYPE_OUT_ID);
		billDetail6.setMaterielId(TEST_MAT_ID_1);
		billDetail6.setAmount(11300.0);
		//billDetail6.setStateId(TEST_MAT_STATE_ID_1);
		bill3.addBillDetail(billDetail6);
		

		Bill billPo3=storageManager.saveBill(bill3);
		
		assertEquals(1,storageManager.findStorageBy("materiel.id", TEST_MAT_ID_1).size());
		Storage storage=storageManager.findBillByMaterielId(TEST_MAT_ID_1);
		assertNotNull(storage);
		assertEquals(Double.valueOf("22500.0"),storage.getAmount());
		
	}

}
